package top.jolyoulu.business.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.jolyoulu.corecommon.queue.delayed.DelayedTaskConsumer;
import top.jolyoulu.corecommon.queue.delayed.DelayedTaskEntity;
import top.jolyoulu.corecommon.queue.delayed.DelayedTaskProducer;
import top.jolyoulu.corecommon.queue.delayed.DelayedTaskQueue;
import top.jolyoulu.corecommon.queue.delayed.observer.AbstractObserver;
import top.jolyoulu.corecommon.queue.delayed.observer.DelayedTaskSubject;
import top.jolyoulu.webcommon.enums.GlobalExpType;
import top.jolyoulu.webcommon.excption.GlobalException;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @Author LuZhouJin
 * @Date 2023/5/4
 * Delayed 测试
 */
@Slf4j
@RestController
@RequestMapping("/test/delayed")
public class TestDelayedTaskController {

    @Autowired
    private DelayedTaskProducer delayedTaskProducer;

    @Autowired
    private DelayedTaskSubject delayedTaskSubject;


    @PostConstruct
    public void postConstruct(){
        //注册一个消费者
        delayedTaskSubject.registerObserver(new AbstractObserver<String>(String.class) {
            @Override
            protected void task(DelayedTaskEntity<String> delayedTask) {
                String task = delayedTask.getTask();
                log.info("执行的任务：{}",task);
            }

        });
    }

    //测试提交一个任务
    @GetMapping("/task/{context}")
    public void Task(@PathVariable("context") String context){
        delayedTaskProducer.put(new DelayedTaskEntity<String>(
                UUID.randomUUID().toString(),
                LocalDateTime.now().plusSeconds(10),
                context
        ));
    }
}
